<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        
    </style>
</head>
<body>
    <input type="button" id="btn" value="按钮1">
    <div class="box"></div>
</body>
<script>
    var box = document.querySelector(".box")

    setTimeout(function(){
        var ipt = document.createElement("input");
        ipt.type = "button";
        ipt.id = "btn2";
        ipt.value = "按钮2";
        box.appendChild(ipt);
    }, 1);

    var btn = document.getElementById("btn");
    // var btn2 = document.getElementById("btn2");
    // console.log(btn2)

    btn.onclick = function(){
        console.log("按钮1");
    }

    // 利用事件委托给页面上暂时不存在的元素绑定事件
    box.onclick = function(eve){
        var e = eve || window.event;
        var target = e.target || e.srcElement;
        if(target.id === "btn2"){
            console.log("按钮2");
        }
    }



    
</script>
</html>